// TODO: 定义宏生成如下结构  
// #[derive_hash(Block)]  
// struct Block { data: String, prev_hash: String }  
// 宏应自动实现 fn hash(&self) -> String  

// 添加宏定义
macro_rules! derive_hash {
    ($struct_name:ident { $($field:ident : $type:ty),* }) => {
        struct $struct_name {
            $($field: $type),*
        }

        impl $struct_name {
            pub fn hash(&self) -> String {
                // 正确的字段顺序：data + prev_hash
                let content = format!("{}{}", self.data, self.prev_hash);
                format!("{:x}", md5::compute(content))
            }
        }
    };
}

// 生成Block结构体
derive_hash!(Block {
    data: String,
    prev_hash: String
});


#[test]  
fn test_block_hash() {  
    let block = Block { 
        data: "Genesis".into(), 
        prev_hash: "0".into() 
    };  
    //assert!(!block.hash().is_empty());  
    assert_eq!(block.hash(), "202c2e169f124362c387f900b1218a43"); // MD5("Genesis0")
}  

// 添加main函数
fn main() {
    let block = Block {
        data: "Test".into(),
        prev_hash: "0000".into()
    };
    println!("Block hash: {}", block.hash());
}